import 'package:flutter/material.dart';
import 'package:kxy_flutter_app/components/exam_card_dot/error_dot.dart';
import 'package:kxy_flutter_app/components/exam_card_dot/right_dot.dart';
import 'package:kxy_flutter_app/components/exam_card_dot/un_answer_dot.dart';
import 'package:kxy_flutter_app/model/exam_question.dart';
import 'package:kxy_flutter_app/view/examination/question_review_view.dart';
import 'package:kxy_flutter_app/view/examination/view_model/test_model.dart';
import 'package:provider/provider.dart';

class TestResultView extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('答题报告'),
      ),
      body: Container(
        margin: EdgeInsets.symmetric(horizontal: 20, vertical: 20),
        child: body(context),
      ),
    );
  }

  Widget body(BuildContext context) {
    return Consumer<TestModel>(
      builder: (cxt, model, child) {
        return Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            resultInfoBody(model),
            btnReuslt(model.questionList, context),
          ],
        );
      },
    );
  }

  Widget resultInfoBody(TestModel model) {
    var counter = Row(
      mainAxisAlignment: MainAxisAlignment.spaceBetween,
      children: [
        countItem(model.testResult.rightQuesCount, 1),
        countItem(model.testResult.errorQuesCount, 2),
        countItem((model.testResult.rightRatio * 100).toInt(), 9),
      ],
    );

    int minutes = (model.testResult.answerTime / 60).floor();
    int seconds = model.testResult.answerTime % 60;
    var text = '用时$minutes分$seconds秒';
    var tip = Padding(
      padding: EdgeInsets.only(top: 20),
      child: Row(
        children: [
          Padding(
            padding: EdgeInsets.only(right: 2),
            child: Icon(Icons.timer, size: 16, color: Colors.black54),
          ),
          Text(text, style: TextStyle(fontSize: 14, color: Colors.black54)),
        ],
      ),
    );

    return Container(
      padding: EdgeInsets.all(20),
      decoration: BoxDecoration(
        color: Colors.white,
        borderRadius: BorderRadius.circular(5),
      ),
      child: Column(
        children: [
          counter,
          tip,
        ],
      ),
    );
  }

  Widget btnReuslt(List<Question> quesList, BuildContext context) {
    return Container(
      height: 40,
      color: Colors.white,
      margin: EdgeInsets.only(top: 40),
      width: MediaQuery.of(context).size.width,
      child: ClipRRect(
        borderRadius: BorderRadius.circular(50),
        child: FlatButton(
          color: Theme.of(context).primaryColor,
          textColor: Colors.white,
          child: Text('查看解析结果'),
          onPressed: () {
            Navigator.of(context).push(
              MaterialPageRoute(
                builder: (cxt) => QuestionReviewView(quesList: quesList),
              ),
            );
          },
        ),
      ),
    );
  }

  // type 0: 未答题
  // type 1: 答对题
  // type 2: 答错题
  Widget countItem(int count, int type) {
    Widget dot;
    if (type == 0) {
      dot = UnAnswerDot();
    } else if (type == 1) {
      dot = RightDot();
    } else if (type == 2) {
      dot = ErrorDot();
    } else if (type == 9) {
      dot = Text('正确率');
    } else {
      dot = Text('未知结果');
    }

    var counter = Row(
      crossAxisAlignment: CrossAxisAlignment.end,
      mainAxisAlignment: MainAxisAlignment.start,
      children: [
        Text(
          count.toString(),
          style: TextStyle(
            fontSize: 24,
            color: Colors.black87,
          ),
        ),
        Text(
          type == 9 ? '%' : '题',
          style: TextStyle(
            fontSize: 14,
            color: Colors.black87,
          ),
        )
      ],
    );

    return Container(
      child: Column(
        mainAxisAlignment: MainAxisAlignment.start,
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          counter,
          Padding(child: dot, padding: EdgeInsets.only(top: 5)),
        ],
      ),
    );
  }
}
